import facade
from parsel import Selector
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
topPath = BaseDir.get_upper_dir(curPath, -2)
filepath = BaseDir.get_new_path(topPath, "download", "wanfang_bz", "download", "page")
filepath2 = BaseDir.get_new_path(topPath, "download", "wanfang_bz", "download", "pagetechstreet")
configfile = BaseDir.get_new_path(curPath, "db.ini")


class ParsingSaveID(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)

    def parsing(self):
        self.allcount = 0
        for file in BaseDir.get_dir_all_files(filepath2):
            print(file)
            text = BaseDir.single_read_file(file)
            selector = Selector(text=text)
            divselect = selector.xpath('/html/body/div/div[@class="ResultList "]')
            values = []
            count = 0
            for div in divselect:
                longid = div.xpath(".//div[2]/div[1]/a[1]/@href").get().replace("/link.do?url=", "")
                standardid = div.xpath(".//div[2]/div[1]//i[contains(@id, 'icon')]/@onclick").get()
                standardid = standardid.replace("showBox(this.id,'", "").replace("','standards')", "")
                print((longid, standardid))
                # values.append((longid, standardid, file))
                values.append((longid, standardid))
                count += 1
            print(count)
            self.allcount += count
            print(self.allcount)
            # sql = "insert ignore into articlenew1 (longid,standardid,filepath) values (%s,%s,%s)"
            sql = "insert ignore into articlenew (longid,standardid) values (%s,%s)"
            self.mysqlutils.ExeSqlMany(sql, values)


if __name__ == "__main__":
    pars = ParsingSaveID()
    pars.parsing()
